package com.collect.senegal.zhiScene.zhiWidget.zhiFilterTelemarketing;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.collect.senegal.MyApplication;
import com.collect.senegal.R;
import com.collect.senegal.zhiAdater.zhiFilterDaily.ZHIFilterConditionAdjuster;
import com.collect.senegal.zhiBean.zhiReceive.ZHIRFilterEntity;
import com.collect.senegal.zhiScene.zhiWidget.zhiFilterDaily.ZHIFilterDailyClickListener;
import com.collect.senegal.zhiScene.zhiWidget.zhiFilterDaily.ZHIRangeDateSelect;
import com.collect.senegal.zhiScene.zhiWidget.zhiFilterDaily.ZHIRangeMobileInput;
import com.collect.senegal.zhiUtils.StringUtil;
import com.collect.senegal.zhiUtils.ZHICalendarUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

@SuppressLint("NonConstantResourceId")
public class ZHIFilterTelemaketing extends RelativeLayout {

    private final Context mContext;

//    private RecyclerView rc_type_list;
    private RecyclerView rc_client_list;
    private RecyclerView rc_status_list;

    private ZHIRangeDateSelect rds_due_date;
    private ZHIRangeMobileInput rds_user_mobile;

    private ZHIFilterConditionAdjuster mZHIFilterOrderTypeAdjuster;
    private ArrayList<ZHIRFilterEntity> mOrderTypeFilterArrayList;

    private ZHIFilterConditionAdjuster mZHIFilterClientAdjuster;
    private ArrayList<ZHIRFilterEntity> mClientFilterArrayList;

    private ZHIFilterConditionAdjuster mZHIFilterStatusAdjuster;
    private ArrayList<ZHIRFilterEntity> mStatusArrayList;

    private ZHIFilterDailyClickListener mZHIFilterDailyClickListener;

    public ZHIFilterTelemaketing(Context context) {
        this(context, null);
    }

    public ZHIFilterTelemaketing(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ZHIFilterTelemaketing(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        this.mContext = context;

        initView();
    }

    private void initView() {
        try {
            View mView = View.inflate(mContext, R.layout.view_filter_telemarketing, this);

//            rc_type_list = mView.findViewById(R.id.rc_type_list);
            rc_client_list = mView.findViewById(R.id.rc_client_list);
            rc_status_list = mView.findViewById(R.id.rc_status_list);

            rds_due_date = mView.findViewById(R.id.rds_due_date);
            rds_user_mobile = mView.findViewById(R.id.rds_user_mobile);

            TextView tv_reset = mView.findViewById(R.id.tv_reset);
            tv_reset.setOnClickListener(v -> {
//                for (ZHIRFilterEntity zhirFilterEntity1 : mOrderTypeFilterArrayList) {
//                    zhirFilterEntity1.setChecked(zhirFilterEntity1.getLevel().equalsIgnoreCase(mContext.getResources().getString(R.string.today_all_orders)));
//                }
//                mZHIFilterOrderTypeAdjuster.notifyDataSetChanged();

                for (ZHIRFilterEntity zhirFilterEntity1 : mClientFilterArrayList) {
                    zhirFilterEntity1.setChecked(zhirFilterEntity1.getLevel().equalsIgnoreCase(mContext.getResources().getString(R.string.today_all_orders)));
                }
                mZHIFilterClientAdjuster.notifyDataSetChanged();

                for (ZHIRFilterEntity zhirFilterEntity1 : mStatusArrayList) {
                    zhirFilterEntity1.setChecked(zhirFilterEntity1.getLevel().equalsIgnoreCase("Unfollowed"));
                }
                mZHIFilterStatusAdjuster.notifyDataSetChanged();

                rds_user_mobile.reset();

                initDueDateDataSource();
            });

            RelativeLayout rl_close_bg = mView.findViewById(R.id.rl_close_bg);
            rl_close_bg.setOnClickListener(v -> {
                if (mZHIFilterDailyClickListener != null) {
                    mZHIFilterDailyClickListener.onCloseClick();
                }
            });

            TextView tv_submit = mView.findViewById(R.id.tv_submit);
            tv_submit.setOnClickListener(v -> {
                if (mZHIFilterDailyClickListener != null) {
                    mZHIFilterDailyClickListener.onConfirmClick();
                }
            });

            ////////////////////////////////////
            initDueDatePicker();

            ////////////////////////////////////
            initOrderTypeAdjuster();

            ////////////////////////////////////
            initClientAdjuster();

            ////////////////////////////////////
            initStatusAdjuster();

            ////////////////////////////////////
            initDueDateDataSource();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @SuppressLint("SimpleDateFormat")
    private void initDueDatePicker() {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String gdate = simpleDateFormat.format(new Date());
            String g5date = ZHICalendarUtil.getDateStringAfterDate(gdate, -5);
            rds_due_date.initTimePicker(g5date + " 00:00:00");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initOrderTypeAdjuster() {
//        String[] datas = new String[]{
//                mContext.getResources().getString(R.string.today_all_orders),
//                mContext.getResources().getString(R.string.did_followed),
//                mContext.getResources().getString(R.string.not_followed)};
//
//        String[] dataValues = new String[]{
//                "",
//                "YES",
//                "NO"};
//
//        mOrderTypeFilterArrayList = new ArrayList<>();
//        for (int i = 0; i < datas.length; i++) {
//            ZHIRFilterEntity zhirFilterEntity = new ZHIRFilterEntity();
//            zhirFilterEntity.setLevel(datas[i]);
//            zhirFilterEntity.setDateValue(dataValues[i]);
//            zhirFilterEntity.setChecked(i == 2);
//            mOrderTypeFilterArrayList.add(zhirFilterEntity);
//        }
//
//        mZHIFilterOrderTypeAdjuster = new ZHIFilterConditionAdjuster();
//        mZHIFilterOrderTypeAdjuster.setOnZHIFilterAdjusterItemClickListener((zhirFilterEntity, position) -> {
//            if (zhirFilterEntity != null) {
//                for (ZHIRFilterEntity zhirFilterEntity1 : mOrderTypeFilterArrayList) {
//                    if (zhirFilterEntity1.getLevel().equals(zhirFilterEntity.getLevel())) {
//                        zhirFilterEntity1.setChecked(!zhirFilterEntity1.isChecked());
//
//                    } else {
//                        zhirFilterEntity1.setChecked(false);
//                    }
//                }
//                mZHIFilterOrderTypeAdjuster.notifyDataSetChanged();
//            }
//        });
//        rc_type_list.setLayoutManager(new GridLayoutManager(mContext, 3));
//        rc_type_list.setAdapter(mZHIFilterOrderTypeAdjuster);
//        mZHIFilterOrderTypeAdjuster.setNewData(mOrderTypeFilterArrayList);
    }

    private void initClientAdjuster() {
        String[] datas = new String[]{
                "All",
                "Good Client",
                "Not Reloan",
                "Not apply"};

        //默认传空串,GOOD_CLIENT,NOT_RELOAN,NOT_APPLY
        String[] dataValues = new String[]{
                "",
                "GOOD_CLIENT",
                "NOT_RELOAN",
                "NOT_APPLY"};

        mClientFilterArrayList = new ArrayList<>();
        for (int i = 0; i < datas.length; i++) {
            ZHIRFilterEntity zhirFilterEntity = new ZHIRFilterEntity();
            zhirFilterEntity.setLevel(datas[i]);
            zhirFilterEntity.setDateValue(dataValues[i]);
            zhirFilterEntity.setChecked(i == 0);
            mClientFilterArrayList.add(zhirFilterEntity);
        }

        mZHIFilterClientAdjuster = new ZHIFilterConditionAdjuster();
        mZHIFilterClientAdjuster.setOnZHIFilterAdjusterItemClickListener((zhirFilterEntity, position) -> {
            if (zhirFilterEntity != null) {
                for (ZHIRFilterEntity zhirFilterEntity1 : mClientFilterArrayList) {
                    if (zhirFilterEntity1.getLevel().equals(zhirFilterEntity.getLevel())) {
                        zhirFilterEntity1.setChecked(!zhirFilterEntity1.isChecked());

                    } else {
                        zhirFilterEntity1.setChecked(false);
                    }
                }
                mZHIFilterClientAdjuster.notifyDataSetChanged();
            }
        });
        rc_client_list.setLayoutManager(new GridLayoutManager(mContext, 3));
        rc_client_list.setAdapter(mZHIFilterClientAdjuster);
        mZHIFilterClientAdjuster.setNewData(mClientFilterArrayList);
    }

    private void initStatusAdjuster() {
        String[] datas = new String[]{
                MyApplication.getAppContext().getString(R.string.unfollowed),
                MyApplication.getAppContext().getString(R.string.follow),
                MyApplication.getAppContext().getString(R.string.focus_on)};

        //默认UNFOLLOWED,FOLLOWED,FOCUS_ON
        String[] dataValues = new String[]{
                "UNFOLLOWED",
                "FOLLOWED",
                "FOCUS_ON"};

        mStatusArrayList = new ArrayList<>();
        for (int i = 0; i < datas.length; i++) {
            ZHIRFilterEntity zhirFilterEntity = new ZHIRFilterEntity();
            zhirFilterEntity.setLevel(datas[i]);
            zhirFilterEntity.setDateValue(dataValues[i]);
            zhirFilterEntity.setChecked(i == 0);
            mStatusArrayList.add(zhirFilterEntity);
        }

        mZHIFilterStatusAdjuster = new ZHIFilterConditionAdjuster();
        mZHIFilterStatusAdjuster.setOnZHIFilterAdjusterItemClickListener((zhirFilterEntity, position) -> {
            if (zhirFilterEntity != null) {
                for (ZHIRFilterEntity zhirFilterEntity1 : mStatusArrayList) {
                    if (zhirFilterEntity1.getLevel().equals(zhirFilterEntity.getLevel())) {
                        zhirFilterEntity1.setChecked(!zhirFilterEntity1.isChecked());

                    } else {
                        zhirFilterEntity1.setChecked(false);
                    }
                }
                mZHIFilterStatusAdjuster.notifyDataSetChanged();
            }
        });
        rc_status_list.setLayoutManager(new GridLayoutManager(mContext, 3));
        rc_status_list.setAdapter(mZHIFilterStatusAdjuster);
        mZHIFilterStatusAdjuster.setNewData(mStatusArrayList);
    }

    private void initDueDateDataSource() {
        try {

            Date curDate = new Date();
            rds_due_date.updatePromisedDueTimeStart(false, curDate);

            rds_due_date.updatePromisedDueTimeEnd(false, curDate);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setZHIFilterDailyClickListener(ZHIFilterDailyClickListener listener) {
        this.mZHIFilterDailyClickListener = listener;
    }

    public String getOrderType() {

        String tResult = "";
//        if (mOrderTypeFilterArrayList != null) {
//            for (ZHIRFilterEntity zhirFilterEntity : mOrderTypeFilterArrayList) {
//                if (zhirFilterEntity.isChecked()) {
//                    tResult = zhirFilterEntity.getDateValue();
//                    break;
//                }
//            }
//        }

        return tResult;
    }

    public String getClient() {

        String tResult = "";
        if (mClientFilterArrayList != null) {
            for (ZHIRFilterEntity zhirFilterEntity : mClientFilterArrayList) {
                if (zhirFilterEntity.isChecked()) {
                    tResult = zhirFilterEntity.getDateValue();
                    break;
                }
            }
        }

        return tResult;
    }

    public String getStatus() {

        String tResult = "";
        if (mStatusArrayList != null) {
            for (ZHIRFilterEntity zhirFilterEntity : mStatusArrayList) {
                if (zhirFilterEntity.isChecked()) {
                    tResult = zhirFilterEntity.getDateValue();
                    break;
                }
            }
        }

        return tResult;
    }

    public String getAssignStartDate() {
        return StringUtil.safeString(rds_due_date.getStartDate());
    }

    public String getAssignEndDate() {
        return StringUtil.safeString(rds_due_date.getEndDate());
    }

    public String getUserMobile() {
        return StringUtil.safeString(rds_user_mobile.getMobile());
    }
}
